package cate.game.play.skill.passive.hero;

import cate.common.util.XT;
import cate.game.play.buff.Buff;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.List;

/**
 * 每回合开始时，如果自身的增益状态数量多于减益状态数量，则每多1个状态，获得1层【津津有味】，最多4层。
 *  获得10层【津津有味】后进化为【八珍玉食】，再获得10层【津津有味】可进化为【饕餮盛宴】。不再获得【津津有味】，不可驱散，持续到战斗结束。
 */
public class 饕餮盛宴PH extends PassiveHandler {

	private int roundBuff;

	private int 回合上限;

	private int 最大层数;

	private List<Integer> 进化buff;

	//buff=？？&回合上限=4&最大层数=10&进化buff=??#??
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		roundBuff = args.getInt("buff", 0);
		回合上限 = args.getInt("回合上限", 0);
		最大层数 = args.getInt("最大层数", 0);
		进化buff = args.getIntList("进化buff");
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		if (skill.owner.isDead()) {
			return;
		}
		if (stopAdd) {
			return;
		}
		int goodNum = 0;
		int badNum = 0;
		for (Buff buff : skill.owner.buff.getValidBuffs()) {
			if (!buff.cfg.row.notice) {
				continue;
			}
			if (buff.isGood()) {
				goodNum++;
			} else {
				badNum++;
			}
		}
		int addNum = Math.min(回合上限, goodNum - badNum);
		if (addNum <= 0) {
			return;
		}
		addBuff(action,addNum);
	}

	private void addBuff(ActionCtx action, int addNum) {
		int curBuffNum = skill.owner.buff.countBuffByTid(roundBuff);
		while (addNum-- > 0 && !stopAdd) {
			int i = skill.owner.buff.tryAddByTid(action, roundBuff);
			if (i > 0) {
				curBuffNum++;
				if (curBuffNum >= 最大层数) {
					int nextBuff = getNextBuff();
					if (nextBuff <= 0) {
						stopAdd = true;
						return;
					}
					curIndex++;
					skill.owner.buff.removeBuffByTid(action, roundBuff);
					curBuffNum = 0;
					skill.owner.buff.tryAddByTid(action, nextBuff);
					if (getNextBuff() <= 0) {
						stopAdd = true;
					}
				}
			}
		}
	}


	private boolean stopAdd;
	private int curIndex = -1;

	private int getNextBuff() {
		if (XT.isBlank(进化buff)) {
			return -1;
		}
		if (curIndex + 1 >= 进化buff.size()) {
			return -1;
		}
		return 进化buff.get(curIndex + 1);
	}

}
